package main_task

import (
	"go_server/game_server/api/service"
	"go_server/game_server/constants"
	"go_server/game_server/event_manager"
	"go_server/game_server/event_manager/event_data"
	"go_server/game_server/event_manager/event_data/event_impl"
	"go_server/zinx/zlog"
	"math"
)

type MainTaskService struct {
	service.AbsService

	GetMainTaskChapterInfo *GetMainTaskChapterInfoApi
	GetMainTaskPrize       *GetMainTaskPrizeApi

	eventMethods map[event_data.EventType]func(event event_data.IEvent)
}

var (
	ServiceObj *MainTaskService
)

func init() {
	ServiceObj = &MainTaskService{
		GetMainTaskChapterInfo: &GetMainTaskChapterInfoApi{},
		GetMainTaskPrize:       &GetMainTaskPrizeApi{},

		eventMethods: make(map[event_data.EventType]func(event event_data.IEvent)),
	}

	event_manager.EventManagerObj.AddServiceEvent(event_data.DreamSceneBattleEvent, onDreamSceneBattleEvent, ServiceObj)         //梦境关卡战斗事件
	event_manager.EventManagerObj.AddServiceEvent(event_data.CleanUpDebrisEvent, onCleanUpDebrisEvent, ServiceObj)               //铲除杂物
	event_manager.EventManagerObj.AddServiceEvent(event_data.SummonHeroEvent, onSummonHeroEvent, ServiceObj)                     //英雄招募
	event_manager.EventManagerObj.AddServiceEvent(event_data.UpLevelHeroEvent, oNUpLevelHeroEvent, ServiceObj)                   //英雄升级
	event_manager.EventManagerObj.AddServiceEvent(event_data.UpStarHeroEvent, onUpStarHeroEvent, ServiceObj)                     //英雄升星
	event_manager.EventManagerObj.AddServiceEvent(event_data.BuildLevelupEvent, onBuildLevelUpEvent, ServiceObj)                 //建筑升级
	event_manager.EventManagerObj.AddServiceEvent(event_data.RepairBuildEvent, onRepairBuildEvent, ServiceObj)                   //建筑修复
	event_manager.EventManagerObj.AddServiceEvent(event_data.ExploreBuildEvent, onExploreBuildEvent, ServiceObj)                 //探索建筑
	event_manager.EventManagerObj.AddServiceEvent(event_data.RepairWallEvent, onRepairWallEvent, ServiceObj)                     //修复城墙
	event_manager.EventManagerObj.AddServiceEvent(event_data.ItemUseEvent, onItemUseEvent, ServiceObj)                           //使用物品
	event_manager.EventManagerObj.AddServiceEvent(event_data.CollectFoodEvent, onCollectFoodEvent, ServiceObj)                   //收集食物
	event_manager.EventManagerObj.AddServiceEvent(event_data.BuildResourceCollectEvent, onBuildResourceCollectEvent, ServiceObj) //资源采集
	event_manager.EventManagerObj.AddServiceEvent(event_data.BuildRescueEvent, onBuildRescueEvent, ServiceObj)                   //建筑营救
	event_manager.EventManagerObj.AddServiceEvent(event_data.BuildSpeedUpEvent, onBuildSpeedUpEvent, ServiceObj)                 //建筑加速
}

func (s *MainTaskService) GetModuleName() string {
	return "main_task"
}

func (s *MainTaskService) GetEventHandleFunc(eventType event_data.EventType) (func(event event_data.IEvent), bool) {
	eventFunc, ok := s.eventMethods[eventType]
	if !ok {
		zlog.Errorf("GetEventHandleFunc Err!!! method not found!! eventType: %d", eventType)
		return nil, false
	}
	return eventFunc, true
}

func (s *MainTaskService) GetEventMap() map[event_data.EventType]func(event event_data.IEvent) {
	return s.eventMethods
}

// 梦境关卡战斗事件
func onDreamSceneBattleEvent(event event_data.IEvent) {
	conn := event.GetRoleConn()
	roleId, ok := event.GetRoleId()
	if !ok {
		return
	}
	rstEvent := event.(*event_impl.DreamSceneBattleEvent)
	params := rstEvent.Params

	var taskType int32 = constants.MAIN_TASK                       // 任务类别-主线任务
	var taskEvent1 int32 = constants.DREAM_SCENE_BATTLE_EVENT      // 任务事件-战斗次数
	var taskEvent2 int32 = constants.DREAM_SCENE_BATTLE_STAR_EVENT // 任务事件-指定关卡获得星级

	ServiceObj.S2C_UpdateMainTask(conn, roleId, taskType, taskEvent1, map[string]int32{
		"id":    0,
		"value": 1, // 完成一次战斗
	})
	ServiceObj.S2C_UpdateMainTask(conn, roleId, taskType, taskEvent2, params)

	//ServiceObj.GetMainTaskPrize.TestHandle(roleId.(int64))
}

// 铲除杂物
func onCleanUpDebrisEvent(event event_data.IEvent) {
	conn := event.GetRoleConn()
	roleId, ok := event.GetRoleId()
	if !ok {
		return
	}

	var taskType int32 = constants.MAIN_TASK           // 任务类别-主线任务
	var taskEvent int32 = constants.CLEAN_DEBRIS_EVENT // 任务事件-铲除杂物

	ServiceObj.S2C_UpdateMainTask(conn, roleId, taskType, taskEvent, map[string]int32{
		"id":    0,
		"value": 1, // 完成一次除草
	})
}

// 英雄招募
func onSummonHeroEvent(event event_data.IEvent) {
	conn := event.GetRoleConn()
	roleId, ok := event.GetRoleId()
	if !ok {
		return
	}
	rstEvent := event.(*event_impl.SummonHeroEvent)
	params := rstEvent.Params

	var taskType int32 = constants.MAIN_TASK          // 任务类别-主线任务
	var taskEvent int32 = constants.SUMMON_HERO_EVENT // 任务事件-英雄招募

	ServiceObj.S2C_UpdateMainTask(conn, roleId, taskType, taskEvent, params)
}

// 英雄升级
func oNUpLevelHeroEvent(event event_data.IEvent) {
	conn := event.GetRoleConn()
	roleId, ok := event.GetRoleId()
	if !ok {
		return
	}
	rstEvent := event.(*event_impl.UpLevelHeroEvent)
	upLevel := rstEvent.AfterLv - rstEvent.BeforeLv // 提升的等级次数

	var taskType int32 = constants.MAIN_TASK            // 任务类别-主线任务
	var taskEvent1 int32 = constants.UPGRADE_HERO_EVENT // 任务事件-英雄升级
	var taskEvent2 int32 = constants.HERO_LEVEL_EVENT   // 任务事件-英雄等级达到X级

	ServiceObj.S2C_UpdateMainTask(conn, roleId, taskType, taskEvent1, map[string]int32{
		"id":    0,
		"value": upLevel, // 升级次数
	})

	ServiceObj.S2C_UpdateMainTask(conn, roleId, taskType, taskEvent2, map[string]int32{
		"id":    0,
		"value": rstEvent.AfterLv, // 英雄达到多少等级,固定值判断
	})
}

// 英雄升星
func onUpStarHeroEvent(event event_data.IEvent) {
	conn := event.GetRoleConn()
	roleId, ok := event.GetRoleId()
	if !ok {
		return
	}
	rstEvent := event.(*event_impl.UpStarHeroEvent)
	upStars := rstEvent.AfterStar - rstEvent.BeforeStar // 提升的星级次数

	var taskType int32 = constants.MAIN_TASK          // 任务类别-主线任务
	var taskEvent int32 = constants.UPSTAR_HERO_EVENT // 任务事件-英雄升星

	ServiceObj.S2C_UpdateMainTask(conn, roleId, taskType, taskEvent, map[string]int32{
		"id":    0,
		"value": upStars, // 升星次数
	})
}

// 建筑升级
func onBuildLevelUpEvent(event event_data.IEvent) {
	conn := event.GetRoleConn()
	roleId, ok := event.GetRoleId()
	if !ok {
		return
	}
	rstEvent := event.(*event_impl.BuildLevelupEvent)

	var taskType int32 = constants.MAIN_TASK            // 任务类别-主线任务
	var taskEvent int32 = constants.UPGRADE_BUILD_EVENT // 任务事件-建筑升到等级级（固定值判断）

	ServiceObj.S2C_UpdateMainTask(conn, roleId, taskType, taskEvent, map[string]int32{
		"id":    rstEvent.CfgBuildId,
		"value": rstEvent.Level, // 建筑当前等级
	})
}

// 建筑修复
func onRepairBuildEvent(event event_data.IEvent) {
	conn := event.GetRoleConn()
	roleId, ok := event.GetRoleId()
	if !ok {
		return
	}
	rstEvent := event.(*event_impl.RepairBuildEvent)

	var taskType int32 = constants.MAIN_TASK           // 任务类别-主线任务
	var taskEvent int32 = constants.REPAIR_BUILD_EVENT // 任务事件-建筑修复

	ServiceObj.S2C_UpdateMainTask(conn, roleId, taskType, taskEvent, map[string]int32{
		"id":    rstEvent.CfgBuildId,
		"value": 1, // 次数
	})
}

// 探索建筑
func onExploreBuildEvent(event event_data.IEvent) {
	conn := event.GetRoleConn()
	roleId, ok := event.GetRoleId()
	if !ok {
		return
	}
	rstEvent := event.(*event_impl.ExploreBuildEvent)

	var taskType int32 = constants.MAIN_TASK            // 任务类别-主线任务
	var taskEvent int32 = constants.EXPLORE_BUILD_EVENT // 任务事件-探索建筑

	ServiceObj.S2C_UpdateMainTask(conn, roleId, taskType, taskEvent, map[string]int32{
		"id":    rstEvent.CfgBuildId,
		"value": 1, // 次数
	})
}

// 修复城墙
func onRepairWallEvent(event event_data.IEvent) {
	conn := event.GetRoleConn()
	roleId, ok := event.GetRoleId()
	if !ok {
		return
	}

	var taskType int32 = constants.MAIN_TASK          // 任务类别-主线任务
	var taskEvent int32 = constants.REPAIR_WALL_EVENT // 任务事件-修复城墙

	ServiceObj.S2C_UpdateMainTask(conn, roleId, taskType, taskEvent, map[string]int32{
		"id":    0,
		"value": 1, // 次数
	})
}

// 使用物品
func onItemUseEvent(event event_data.IEvent) {
	conn := event.GetRoleConn()
	roleId, ok := event.GetRoleId()
	if !ok {
		return
	}
	rstEvent := event.(*event_impl.ItemUseEvent)

	if len(rstEvent.Items) > 0 {
		var taskType int32 = constants.MAIN_TASK           // 任务类别-主线任务
		var taskEvent int32 = constants.RESCUE_SIMBA_EVENT // 任务事件-使用物品

		for key, value := range rstEvent.Items {
			ServiceObj.S2C_UpdateMainTask(conn, roleId, taskType, taskEvent, map[string]int32{
				"id":    key,   // 物品id
				"value": value, // 物品数量
			})
		}
	}
}

// 收集食物
func onCollectFoodEvent(event event_data.IEvent) {
	conn := event.GetRoleConn()
	roleId, ok := event.GetRoleId()
	if !ok {
		return
	}

	var taskType int32 = constants.MAIN_TASK           // 任务类别-主线任务
	var taskEvent int32 = constants.COLLECT_FOOD_EVENT // 任务事件-收集食物

	ServiceObj.S2C_UpdateMainTask(conn, roleId, taskType, taskEvent, map[string]int32{
		"id":    0,
		"value": 1, // 次数
	})
}

// 资源采集
func onBuildResourceCollectEvent(event event_data.IEvent) {
	conn := event.GetRoleConn()
	roleId, ok := event.GetRoleId()
	if !ok {
		return
	}
	rstEvent := event.(*event_impl.BuildResourceCollectEvent)

	var taskType int32 = constants.MAIN_TASK                // 任务类别-主线任务
	var taskEvent int32 = constants.COLLECT_RESOURCES_EVENT // 任务事件-资源采集

	ServiceObj.S2C_UpdateMainTask(conn, roleId, taskType, taskEvent, rstEvent.Params)
}

// 建筑营救
func onBuildRescueEvent(event event_data.IEvent) {
	conn := event.GetRoleConn()
	roleId, ok := event.GetRoleId()
	if !ok {
		return
	}

	var taskType int32 = constants.MAIN_TASK     // 任务类别-主线任务
	var taskEvent int32 = constants.RESCUE_EVENT // 任务事件-建筑营救

	ServiceObj.S2C_UpdateMainTask(conn, roleId, taskType, taskEvent, map[string]int32{
		"id":    0,
		"value": 1, // 次数
	})
}

// 建筑加速
func onBuildSpeedUpEvent(event event_data.IEvent) {
	conn := event.GetRoleConn()
	roleId, ok := event.GetRoleId()
	if !ok {
		return
	}

	rstEvent := event.(*event_impl.BuildSpeedUpEvent)
	if rstEvent.Time > 0 {
		upSpeedTime := rstEvent.Time                                      // 毫秒时间戳
		minute := int32(math.Ceil(float64(upSpeedTime) / float64(60000))) // 转化成分钟数,向上取整

		var taskType int32 = constants.MAIN_TASK       // 任务类别-主线任务
		var taskEvent int32 = constants.SPEED_UP_EVENT // 任务事件-建筑加速

		ServiceObj.S2C_UpdateMainTask(conn, roleId, taskType, taskEvent, map[string]int32{
			"id":    0,
			"value": minute, // 加速分钟数
		})
	}
}
